﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using UcAsp.Models;
namespace UcAsp.Controllers
{
    public class WorksController : Controller
    {
        //
        // GET: /Works/
        private UcAspNetEntities entities = new UcAspNetEntities();
        public ActionResult Index(int? page,int? typeid)
        {
            if(typeid==null||typeid==0)
            {typeid=21;}

            IEnumerable<Articles> query = from c in entities.Articless
                        join m in entities.Categoriess
                        on c.CategorieID equals m.ID
                        where m.ParentID == typeid
                        orderby c.CategorieID,c.ID descending
                        select c;
            if (typeid != 21)
            {
                query = from c in entities.Articless
                        where c.CategorieID == typeid
                        orderby c.CategorieID, c.ID descending
                        select c;
            }
            int pagesize = 36;
            int pageindex = 0;
            if (page == null)
            {
                page = 1;
            }
            pageindex =Convert.ToInt32(page) - 1;
           
            ViewData["PageIndex"] = pageindex + 1;
            ViewData["PageCount"] = (query.Count() + 35) / 36;
            ViewData["TypeID"] = typeid;
            query = query.Skip(pageindex * pagesize).ToList<Articles>().Take(pagesize);
            List<Articles> list = new List<Articles>();
            foreach (Articles a in query)
            {
                a.Categoriess = entities.Categoriess.FirstOrDefault(o => o.ID == a.CategorieID);
                list.Add(a);
            }
            //ViewData["recommend"] = entities.Articless.Where(o => o.CategorieID == 13 && o.IsRecommend == true).Take(4).ToList();
            return View(list);
        }

    }
}
